<template>
  <el-dialog :title="model.id?'编辑用户':'添加用户'" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="modelRules" :model="model" label-width="160px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="model.username" placeholder="用户名"/>
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="model.nickName" placeholder="昵称"/>
      </el-form-item>
      <el-form-item label="选择用户角色" prop="level">
        <el-select v-model="model.level" placeholder="请选择" style="display: block">
          <el-option
            v-for="item in roles"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="最高重试次数（次）" prop="maxRetryTime">
        <el-input v-model.number="model.maxRetryTime" placeholder="最高重试次数"/>
      </el-form-item>
      <el-form-item label="锁定时间（分钟）" prop="lockTimer">
        <el-input v-model.number="model.lockTimer" placeholder="锁定时间"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close()">取消</el-button>
      <el-button type="primary" :loading="isSubmitLoading" @click="save()">提交</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getUserForEdit, createOrUpdateUser } from "@/api/user";

export default {
  data() {
    return {
      dialogVisible: false,
      isSubmitLoading: false,
      roles: [
        {
          value: 2,
          label: '管理员'
        },
        {
          value: 3,
          label: '日志查看员'
        }
      ],
      model: {
        id: undefined,
        username: "",
        nickName: "",
        level: 2,
        enabled: true,
        maxRetryTime: 5,
        lockTimer: 5
      },
      modelRules: {
        username: [{ required: true, trigger: "blur", message: "请输入用户名" }],
        nickName: [{ required: true, trigger: "blur", message: "请输入昵称" }],
        level: [{ required: true, trigger: "select", message: "请选择角色" }],
        maxRetryTime: [{ required: true, trigger: "blur", message: "请输入最高重试次数" }],
        lockTimer: [{ required: true, trigger: "blur", message: "请输入锁定时间" }]
      }
    };
  },
  methods: {
    show(id) {
      this.resetModal();
      this.isSubmitLoading = false;
      if (id) {
        getUserForEdit(id).then(result => {
          this.model = result;
          this.dialogVisible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].clearValidate();
          });
        });
      } else {
        this.dialogVisible = true;
        this.$nextTick(() => {
          this.$refs["dataForm"].clearValidate();
        });
      }
    },
    resetModal() {
      this.model = {
        id: undefined,
        username: "",
        nickName: "",
        level: 2,
        enabled: true,
        maxRetryTime: 5,
        lockTimer: 5
      };
    },
    save() {
      this.isSubmitLoading = true;
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          createOrUpdateUser(this.model)
            .then(() => {
              this.$message({ message: this.model.id ? "编辑成功" : "添加成功", type: "success" });
              this.isSubmitLoading = false; // 无论成功失败，都把按钮的loading状态取消
              this.close();
              this.$emit("save");
            })
            .catch(() => {
              this.isSubmitLoading = false; // 无论成功失败，都把按钮的loading状态取消
            });
        } else {
          this.isSubmitLoading = false; // 无论成功失败，都把按钮的loading状态取消
          this.$message({ message: this.$t("formIsNotValidMessage"), type: "warning" });
        }
      });
    },
    close() {
      this.dialogVisible = false;
      this.isSubmitLoading = false; // 无论成功失败，都把按钮的loading状态取消
    }
  }
};
</script>
